using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace LDNS.SDK.Commands.Models
{
    /// <summary>
    /// 获取当前配置命令类
    /// 作者: huobing
    /// </summary>
    public sealed class GetConfigCommand : BaseCommand<GetConfigResult>
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public const string NAME = "GET_CONFIG";
        
        /// <summary>
        /// 获取命令名称
        /// </summary>
        public override string Name => NAME;
        
        /// <summary>
        /// 获取命令格式
        /// </summary>
        public override string Format => "GET_CONFIG";
        
        /// <summary>
        /// 获取命令描述
        /// </summary>
        public override string Description => "获取当前配置";
        
        /// <summary>
        /// 获取命令权限级别
        /// </summary>
        public override CommandPermission Permission => CommandPermission.User;
        
        /// <summary>
        /// 验证命令参数
        /// </summary>
        /// <param name="args">命令参数数组</param>
        /// <param name="error">错误信息输出</param>
        /// <returns>验证是否通过</returns>
        public override bool Validate(string[] args, out string error) 
        {
            error = string.Empty;
            return args.Length == 0 || (error = "此命令不接受参数") == string.Empty;
        }
    }
    
    /// <summary>
    /// 获取配置命令的返回实体类
    /// 作者: huobing
    /// </summary>
    public class GetConfigResult : BaseCommandResult
    {
        /// <summary>
        /// DNS服务端口
        /// </summary>
        public int Port { get; set; } = 53;

        /// <summary>
        /// 是否启用DNS缓存
        /// </summary>
        public bool EnableCache { get; set; } = true;

        /// <summary>
        /// DNS缓存过期时间（分钟）
        /// </summary>
        public int CacheExpirationMinutes { get; set; } = 60;

        /// <summary>
        /// 最大连接数
        /// </summary>
        public int MaxConnections { get; set; } = 100;

        /// <summary>
        /// 是否启用Socket管理
        /// </summary>
        public bool EnableSocketManagement { get; set; } = false;
        
        /// <summary>
        /// Socket管理端口
        /// </summary>
        public int SocketManagementPort { get; set; } = 8053;
        
        /// <summary>
        /// 是否启用访问控制
        /// </summary>
        public bool EnableAccessControl { get; set; } = false;

        /// <summary>
        /// 允许访问的IP列表（支持单个IP、CIDR 或 IP段，如 192.168.1.0/24 或 192.168.1.10-192.168.1.20）
        /// </summary>
        public List<string> AllowedIPs { get; set; } = ["127.0.0.1"];

        /// <summary>
        /// 默认DNS记录TTL（秒）
        /// </summary>
        public int DefaultRecordTtl { get; set; } = 3600;

        /// <summary>
        /// 上游DNS服务器列表
        /// </summary>
        public List<string> UpstreamDnsServers { get; set; } = [];

        /// <summary>
        /// 控制台输出格式化
        /// </summary>
        /// <returns>格式化后的配置信息</returns>
        public override string ToConsoleString()
        {  
            var sb = new StringBuilder();
            sb.AppendLine("DNS服务配置信息：");
            sb.AppendLine($"  监听端口: {Port}");
            sb.AppendLine($"  启用缓存: {EnableCache}");
            sb.AppendLine($"  缓存过期时间(分钟): {CacheExpirationMinutes}");
            sb.AppendLine($"  最大连接数: {MaxConnections}");
            sb.AppendLine($"  启用Socket管理: {EnableSocketManagement}");
            sb.AppendLine($"  Socket管理端口: {SocketManagementPort}");
            sb.AppendLine($"  启用访问控制: {EnableAccessControl}");
            sb.AppendLine($"  允许访问的IP列表: {string.Join(", ", AllowedIPs ?? new List<string>())}");
            sb.AppendLine($"  默认DNS记录TTL(秒): {DefaultRecordTtl}");
            sb.AppendLine($"  上游DNS服务器列表: {string.Join(", ", UpstreamDnsServers ?? new List<string>())}");
            return sb.ToString().TrimEnd();
        }
    }
}